﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;

namespace PlanningGIS.ArcGIS.Custom
{
    /// <summary>
    /// 
    /// </summary>
    public static class XzqUtility
    {
        /// <summary>
        /// 
        /// </summary>
        private static IFeatureWorkspace m_FeatureWorkspace = null;
        /// <summary>
        /// 
        /// </summary>
        private static IFeatureClass pFeatClsLev1 = null;//省
        /// <summary>
        /// 
        /// </summary>
        private static IFeatureClass pFeatClsLev2 = null;//市
        /// <summary>
        /// 
        /// </summary>
        private static IFeatureClass pFeatClsLev3 = null;//县
        /// <summary>
        /// 
        /// </summary>
        private static IFeatureClass pFeatClsLev4 = null;//镇

        /// <summary>
        /// 
        /// </summary>
        public static string PathName { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public static IFeatureClass Lev1FeatureClass
        {
            get
            {
                if (pFeatClsLev1 == null)
                    pFeatClsLev1 = GetXZQFeatureClass("LEV1");
                return pFeatClsLev1;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static IFeatureClass Lev2FeatureClass
        {
            get
            {
                if (pFeatClsLev2 == null)
                    pFeatClsLev2 = GetXZQFeatureClass("LEV2");
                return pFeatClsLev2;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static IFeatureClass Lev3FeatureClass
        {
            get
            {
                if (pFeatClsLev3 == null)
                    pFeatClsLev3 = GetXZQFeatureClass("LEV3");
                return pFeatClsLev3;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static IFeatureClass Lev4FeatureClass
        {
            get
            {
                if (pFeatClsLev4 == null)
                    pFeatClsLev4 = GetXZQFeatureClass("LEV4");
                return pFeatClsLev4;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strFeatureClassName"></param>
        /// <returns></returns>
        private static IFeatureClass GetXZQFeatureClass(string strFeatureClassName)
        {
            try
            {
                IWorkspace2 pWorkspace2 = m_FeatureWorkspace as IWorkspace2;
                if (m_FeatureWorkspace == null && Directory.Exists(PathName))
                {
                    IWorkspaceFactory m_WorkspaceFactory = new FileGDBWorkspaceFactory();
                    m_FeatureWorkspace = m_WorkspaceFactory.OpenFromFile(PathName, 0) as IFeatureWorkspace;
                }
                if (m_FeatureWorkspace == null) return null ;
                IWorkspace2 pWks2 = m_FeatureWorkspace as IWorkspace2;
                if (pWks2.get_NameExists(esriDatasetType.esriDTFeatureClass, strFeatureClassName) == false) return null;
                return m_FeatureWorkspace.OpenFeatureClass(strFeatureClassName);
            }
            catch
            {

            }
            return null;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="xzqCode"></param>
        /// <param name="outName"></param>
        /// <returns></returns>
        public static IPolygon GetXzqByCode(string xzqCode, ref string outName)
        {
            if (Lev1FeatureClass == null) return null;
            IPolygon pPolygon = GetGeometry(Lev1FeatureClass, xzqCode, ref outName);   
            if (pPolygon == null)
            {
                if (Lev2FeatureClass == null) return null;
                pPolygon = GetGeometry(Lev2FeatureClass, xzqCode, ref outName);
                if (pPolygon == null)
                {
                    if (Lev3FeatureClass == null) return null;
                    pPolygon = GetGeometry(Lev3FeatureClass, xzqCode, ref outName);
                    if (pPolygon == null)
                    {
                        if (Lev4FeatureClass == null) return null;
                        pPolygon = GetGeometry(Lev4FeatureClass, xzqCode, ref outName);
                    }
                }
            }
            return pPolygon;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pFeatureClass"></param>
        /// <param name="strCode"></param>
        /// <param name="outName"></param>
        /// <returns></returns>
        private static IPolygon GetGeometry(IFeatureClass pFeatureClass, string strCode, ref string outName)
        {
            IQueryFilter pQueryFilter = new QueryFilter();
            pQueryFilter.SubFields = "*";
            pQueryFilter.WhereClause = "Code='" + strCode + "'";
            IFeatureCursor pFeatureCursor = pFeatureClass.Search(pQueryFilter, false);
            IFeature pFeature =pFeatureCursor.NextFeature();
            if (pFeature != null)
            {
                outName = pFeature.get_Value(pFeature.Fields.FindField("NAME")).ToString();
                return pFeature.ShapeCopy as IPolygon;
            }
            return null;
        }

    }
}
